package Common 
{
	import Core.ImageContainer;
	import Core.ImageUtils;
	import Events.ImageProgressEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.getTimer;
	import Fonts.CalibriEmbed;
	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class CommonMessage extends AbstractMessage 
	{
		private var FCurrentImage:ImageContainer;
		private var FImage:Bitmap;
		private var FMaxCount:int;
		private var FIndex:int;
		private var FStatus:TextField;
		private var FBarBackground:Sprite;
		private var FBar:Sprite;
		private var FTime:int;
		
		public function CommonMessage() 
		{
			super();
			FHeadrer.text = 'Обновление фото: 0 из 0';
			FStatus = new TextField();
			FStatus.defaultTextFormat = FFormat;
			FStatus.mouseEnabled = FStatus.selectable = false;
			addChild(FStatus);
			addChild(FText);
			
			FBarBackground = new Sprite();
			FBarBackground.graphics.beginFill(0xCCCCCC);
			FBarBackground.graphics.drawRect(0, 0, 450, 32);
			FBarBackground.graphics.endFill();
			FBarBackground.filters = [new DropShadowFilter(0, 0, 0, 1, 10, 10, 1, 3, true)];			
			
			FBar = new Sprite();
			FBar.graphics.beginFill(0x333333);
			FBar.graphics.drawRect(0, 0, 450, 32);
			FBar.graphics.endFill();			
			addChild(FBar);
			FBarBackground.alpha = 0.5;
			addChild(FBarBackground);
			
			FBarBackground.x = (FWidth - FBarBackground.width) / 2 ;
			FBarBackground.y = FHeight - FBarBackground.height - FBarBackground.x;
			
			FBar.x = (FWidth - FBar.width) / 2 ;
			FBar.y = FHeight - FBar.height - FBar.x;
			FBar.scaleX = 0;
			Resize();
		}
		
		private function OnEnterFrame(e:Event):void 
		{
			if (FTime < 10)
			{
				FTime++;
				FBar.scaleX = FTime / 10;
			}
			else
				removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
		}
		
		override protected function SetSizes():void
		{
			FWidth = 512;
			FHeight = 236;			
		}
		
		override public function Resize():void 
		{			
			FHeadrer.height = FHeadrer.textHeight;
			FHeadrer.width = FWidth;			
			FStatus.visible = true;
			FStatus.x = 10;
			FStatus.y = 120;
			FStatus.text = 'Обновление базы данных...' + '\n';		
			FStatus.text += '';
			FStatus.width = FWidth;
			FStatus.height = FStatus.textHeight * 3;
			FText.x = 110;
			FText.y = 30;
			FText.width = 500;
			FText.height = 100;
		}
		
		public function set Image(AImage:BitmapData):void
		{
			if (!FImage)	
				FImage = new Bitmap(AImage);
			else
				FImage.bitmapData = AImage;
			ImageUtils.Resize(FImage, 100, 100);
			addChild(FImage);
			FImage.x = 10;
			FImage.y = 30;			
			Resize();
		}		
		
		public function set MaximalPhotos(ACount:int):void
		{
			FMaxCount = ACount;
			FHeadrer.text = 'Обновление фото: ' + FIndex +  ' из ' + FMaxCount;
		}
		
		public function set CurrentPhotoIndex(AIndex:int):void
		{
			FIndex = AIndex;
			FHeadrer.text = 'Обновление фото: ' + FIndex +  ' из ' + FMaxCount;
		}
		
		public function get CurrentImage():ImageContainer 
		{
			return FCurrentImage;
		}
		
		public function set CurrentImage(value:ImageContainer):void 
		{
			FBar.scaleX = 0;
			FTime = 0;
			removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
			if (FCurrentImage)
			{
				FCurrentImage.removeEventListener(ImageProgressEvent.PROGRESS, OnUploadingProgress);
			}
			FCurrentImage = value;
			if (FCurrentImage.IsLocal)
			{
				FStatus.text = 'Уменьшение фото...' + '\n';
				FCurrentImage.addEventListener(ImageProgressEvent.PROGRESS, OnUploadingProgress);
			}
			else
			{
				FStatus.text = 'Обновление базы данных...' + '\n';
				addEventListener(Event.ENTER_FRAME, OnEnterFrame);
			}
			Description	= FCurrentImage.Description;
			Image = FCurrentImage.Photo.bitmapData;
		}		
		
		override public function set Description(ADescription:String):void
		{
			if(ADescription)
				ADescription != '' ? FText.text = ADescription : FText.text = 'Нет описания';
			else
				FText.text = 'Нет описания';
		}
		
		private function OnUploadingProgress(e:ImageProgressEvent):void 
		{
			FBar.scaleX = Math.min(e.Progress, 1);
			if (FBar.scaleX < 0.75)
			{				
				FStatus.text = 'Уменьшение фото...' + Number(FBar.scaleX * 100).toFixed() + '%' + '\n';
			}
			else
			{
				FStatus.text = 'Уменьшение фото...100%' + '\n';
				FStatus.text += 'Зазгрузка на сервер...';
			}
		}
	}
}